package com.itheima.test;

import java.util.Random;

public class MyThread extends Thread {
    //红包总金额
    static double money = 100;
    //红包个数
    static  int count = 3;

    //最小的中奖金额
     private static final double MIN = 0.01;

    @Override
    public void run() {

        //同步代码块
        synchronized (MyThread.class){
            //判断是否还有红包
            if (count == 0){
                System.out.println(getName()+"没有抢到红包");
            }else {
                //定义一个变量表示中将金额
                double prize = 0;
                //判断是否为最后一个红包
                if (count == 1){
                    //最后一个红包的金额为剩余金额
                    prize = money;
                }else {
                    //创建随机数
                    Random r = new Random();
//                    double v = money - (count - 1) * MIN;
                    //随机红包金额
//                    prize = r.nextDouble(v);
                    //判断金额是否小于最小数
                    if (prize < MIN) {
                        prize = MIN;
                    }
                }
                money = money - prize;
                count--;
                System.out.println(getName() + "抢到了" +prize+ "元");
            }


        }

    }
}
